package logic

import (
	"context"
	"rental/models/mysql"

	"rental/rentalrpc/internal/svc"
	"rental/rentalrpc/rentalrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductListLogic {
	return &ProductListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductListLogic) ProductList(in *rentalrpc.ProductListRequest) (*rentalrpc.ProductListResponse, error) {
	// todo: add your logic here and delete this line

	var product []mysql.Product
	mysql.DB.Debug().Find(&product)

	var productList []*rentalrpc.ProductList
	for _, m := range product {

		list := rentalrpc.ProductList{
			Image: m.Image,
			Name:  m.Name,
			Price: float32(m.Price),
			Stick: m.Stock,
		}
		productList = append(productList, &list)
	}

	return &rentalrpc.ProductListResponse{
		List: productList,
	}, nil
}
